#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FolderData;
#endregion

namespace Folder
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {

        #region Fields

        Texture2D tex_selected;

        #endregion


        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
        {
            // Create our menu entries.
            for (int i = 0; i < 4; ++i)
            {
                MenuEntry entry = new MenuEntry("");
                entry.Position = new Vector2(830, 250 + 80 * i);
                entry.isActive = true;
                entry.Font = Fonts.MenuFont;
                MenuEntries.Add(entry);
            }
            UpdateMenuActivation();
            MenuEntries[0].Text = "Prologue";
            MenuEntries[0].Selected += new EventHandler<EventArgs>(Preface_Selected);
            MenuEntries[1].Text = "Contents";
            MenuEntries[1].Selected += new EventHandler<EventArgs>(Contents_Selected);
            MenuEntries[2].Text = "Epilogue";
            MenuEntries[2].Selected += new EventHandler<EventArgs>(Finale_Selected);
            MenuEntries[3].Text = "Authors";
            MenuEntries[3].Selected += new EventHandler<EventArgs>(Author_Selected);

            IsPopup = true;
        }



        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            tex_selected = content.Load<Texture2D>("Menu/title_selected");

            base.LoadContent();
        }


        #region Handle Input

        void Preface_Selected(object sender, EventArgs e)
        {
            if (Session.Progress < 0)
            {
                Session.Progress = 0;
                Session.SaveSession();
            }
            MenuEntries[1].isActive = true;
            NextMenu();
            ScreenManager.AddScreen(new StoryScreen("Story/preface", -1));
        }

        void Finale_Selected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new StoryScreen("Story/Stages/stage6", -1));
        }

        void Contents_Selected(object sender, EventArgs e)
        {
            ExitScreen();
            ScreenManager.AddScreen(new ContentsScreen(0));
        }

        void Author_Selected(object sender, EventArgs e)
        {
            ExitScreen();
            ScreenManager.AddScreen(new AuthorScreen());
        }


        #endregion


        #region Update and Draw

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            UpdateMenuActivation();

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        private void UpdateMenuActivation()
        {

            if (Session.Progress == -1)
            {
                MenuEntries[1].isActive = false;
                MenuEntries[2].isActive = false;
            }
            else if (Session.Progress < 7)
            {
                MenuEntries[2].isActive = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            spriteBatch.DrawString(Fonts.TitleFont, "Folder", new Vector2(750, 100), InverseTransitionColor);
            // Draw each menu entry in turn.
            for (int i = 0; i < MenuEntries.Count; i++)
            {
                MenuEntry menuEntry = MenuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);
                if (isSelected)
                {
                    menuEntry.Draw(this, InverseTransitionColor);
                    spriteBatch.Draw(tex_selected, menuEntry.Position - new Vector2(70, -30), TransitionColor);
                }
                else if (menuEntry.isActive)
                {
                    menuEntry.Draw(this, InverseTransitionColor);
                }
                else
                {
                    menuEntry.Draw(this, InverseTransitionColorDisabled);
                }
            }
            
            spriteBatch.End();
        }

        #endregion
    }
}
